package com.itheima.edu.info.maneger.dao;

import com.itheima.edu.info.maneger.domain.Teacher;

public class TeacherDao implements BaseTeacherDao{
    //1.创建Teacher学生数组长度为5
    private static Teacher[] stus = new Teacher[5];

    static {
        Teacher teacher = new Teacher("0001", "张三", 23, "1991");
        Teacher teacher2 = new Teacher("0002", "李四", 24, "1996");
        Teacher teacher3 = new Teacher("0003", "王五", 25, "1992");
        Teacher teacher4 = new Teacher("0004", "赵六", 23, "1994");

        stus[0] = teacher;
        stus[1] = teacher2;
        stus[2] = teacher3;
        stus[3] = teacher4;
    }

    public Teacher[] findAllTeacher() {
        return stus;
    }


    //2.将接受到的学生对象添加到数组中
    public boolean addTeacher(Teacher teacher) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            //stus[i]
            Teacher stu = stus[i];
            if (stu == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            //装满了
            return false;
        } else {
            //3.返回是否添加成功的boolean类型值
            stus[index] = teacher;
            return true;
        }

    }

    public void deleteTeacher(String id) {
        int index = getIndex(id);
        stus[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Teacher teacher = stus[i];
            if (teacher != null && teacher.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }


    public void updateTeacher(String updateId, Teacher newteacher) {
        int index = getIndex(updateId);
        stus[index] = newteacher;
    }
}
